home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: 73700.776@compuserve.com (Walter C. Riley)
- Newsgroups: comp.lang.c++
- Subject: Re: Syntax for looping through enumerated types
- Date: Wed, 10 Jan 1996 13:34:41 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4d0fe1$947@dub-news-svc-5.compuserve.com>
- References: <4cs70o$8ns@ornews.intel.com>
- NNTP-Posting-Host: dd37-136.compuserve.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- thurman_b_miller@ccm2.hf.intel.com (Thurman Miller) wrote:
-
- >Suppose I have:
-
- >enum daysoftheweek {MONDAY, TUESDAY, WEDNESDAY,THURSDAY,FRIDAY} ;
-
- >How can I loop through this using a for statement?
-
- >(the following doesn't work)
-
- >daysoftheweek nIndex;
- >for (nIndex=MONDAY;nIndex<=FRIDAY;nIndex++)
- >{
-
- > switch (nIndex):
- > {
- > case MONDAY:
- > doMonday();
- > }
-
- >}
-
- >I'd like to get away from specifying the beginning & end (ie MONDAY &
- >TUESDAY) if possible because my enumerated list will constanty grow
- >and I'd like to only change the definition of the enumerated list and
- >the for statement would continue to work.
-
- >Note: This is just a trivial example, not what I'm actually wanting to
- >do.
-
- >TIA
-
- >Thurman Miller
-
- Your code should work - you do have a colon at the end of the switch
- line that does not belong, but I'm guessing that this is a typo. (and
- you do have a main(), don't you? <g>)
-
- If the enum will change often, this is not a good approach, however.
- You likely will be better off using some container class, eg a list,
- array, or dictionary class that best suits what you want to do..
-
- Good luck,
- Walt
-
-
-